/**
 * 
 */
package com.base.myproject.client.busi.b2c.b2cgcodeattr;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BaseStoreEvent;
import com.base.myproject.client.busi.BaseStoreListener;
import com.base.myproject.client.busi.BaseStoreEvent.EventType;
import com.base.myproject.client.jdbc.StoreInfo;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.widget.Html;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Widget;

/**
 * 买过b2ccgcode 商品的人同时在最近还卖过的商品 设计思路：从shoplistorder
 * 里面搜索根据时间排序，最近买过该商品的客户同时还买过什么商品。
 * 在产品上线之初实际上是没有相关的购买商品的，如果在没有相关购买商品的时候，就不要将购买过商品的属性 显示出来。
 * 
 * @author zgw 2009-8-22 下午05:17:06 javazgw@gmail.com
 */
public class AlsoBuy implements B2cGcodeAttr,BaseStoreListener {
	BaseStore<BaseModelData> imagestore;
	String b2cgcode = "";
	int showcount = 5; // 每页显示多少条记录
	String mainhtml="";
	String pangehtml="";
	Html html;
	int limit = 2;
	public AlsoBuy(String b2cgcode) {
		this.b2cgcode = b2cgcode;
		javascript(this);
	}

	Widget getAlsoBuyPanel() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("b2cgcode");
		type.addField("b2cgname");
		type.addField("orderdate");
		type.addField("mainimagepath");

		StoreInfo info = new StoreInfo();
		info.setStart("0");
		info.setLimit(""+limit);
		info.setTablename("b2clistorderg");
		info
				.setSql("select b2cgcode,b2cgname,orderdate,(select mainimagepath from b2cgcode where b2cgcode.b2cgcode =b2clistorderg.b2cgcode ) as mainimagepath from b2clistorderg where b2cgcode='"+getB2cgcodeid()+"'");

		imagestore = new BaseStore<BaseModelData>(type, info);
	
		imagestore.addBaseStoreListener(EventType.LOAD_COMPLETE	, this);
		
		 html = new Html();
//		html.setHtml("<a>asdf</a>");
//		html.
		return html;
	}

	/**
	 * @return the imagestore
	 */
	public BaseStore<BaseModelData> getImagestore() {
		return imagestore;
	}

	/**
	 * @param imagestore the imagestore to set
	 */
	public void setImagestore(BaseStore<BaseModelData> imagestore) {
		this.imagestore = imagestore;
	}

	@Override
	public String getB2cgcodeid() {

		return b2cgcode;
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object getValue() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Widget getWidget() {
		// TODO Auto-generated method stub
		return getAlsoBuyPanel();
	}

	@Override
	public boolean isavtive() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void setActive(boolean b) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setB2cgcodeid(String b2cgcodeid) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setName(String name) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setValue(Object value) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setWidget(Widget w) {
		// TODO Auto-generated method stub

	}

	/**
	 * @return the pangehtml
	 */
	public String getPangehtml() {
		return pangehtml;
	}

	/**
	 * @param pangehtml the pangehtml to set
	 */
	public void setPangehtml(String pangehtml) {
		this.pangehtml = pangehtml;
	}

	@Override
	public void BaseStoreEventHandle(BaseStoreEvent evt) {
		StoreInfo info  = imagestore.getStoreinfo();
		int rowcount =Integer.parseInt(imagestore.getTotlerowcount());//商品总数
		final int limit = Integer.parseInt(info.getLimit());//一页显示商品数量
		int pagecount = rowcount/limit + ((rowcount%limit)==0?0:1);
		
		mainhtml = "<ul id=\"imglist\">";
		pangehtml = "";
		if(rowcount==0)
			return;
		for(int i=0;i<evt.getBasestore().getCount();i++)
		{
			mainhtml +="<li><a href=\"javascript:void(0)\" onclick=\"javascript:_show_also_buy('"+evt.getBasestore().getValue(i, "b2cgcode")+"')\" style=\"table-layout:fixed;word-break:break-all\"><img style=\"width:100px\" src =\""+evt.getBasestore().getValue(i, "mainimagepath")+"\" /></a></li>";
		}
		for(int i=0;i<pagecount;i++)
		{
			
			pangehtml +="<a href=\"javascript:void(0)\" onclick=\"javascript:_show_also_buy_page("+i+")\" >"+(i+1)+"</a>";
			/*Button b = new Button((i+1)+"");
			final int row = i;
			b.addSelectionListener(new SelectionListener<ButtonEvent>(){

				@Override
				public void componentSelected(ButtonEvent ce) {
					// TODO Auto-generated method stub
					
					imagestore.getStoreinfo().setStart((row*limit)+"");
					imagestore.reload();
				}
				
			});*/
//			hp.add(b);
		}
		String total ="<hr /><a>买过这个商品的客户还买过：</a><p>" +mainhtml +"</ul><p>"+ pangehtml+"<hr />";

		html.setHtml(total);
	}

	@Override
	public void onBrowserEvent(Event event) {
		// TODO Auto-generated method stub
		
	}
	
	public  native void javascript(AlsoBuy alsobuy)/*-{
	//
		$wnd._show_also_buy = function(b2cgcode){
		
		alsobuy.@com.base.myproject.client.busi.b2c.b2cgcodeattr.AlsoBuy::showalowbuy(Ljava/lang/String;)(""+b2cgcode+"");

		}
		
			$wnd._show_also_buy_page = function(pagenum){
		
		alsobuy.@com.base.myproject.client.busi.b2c.b2cgcodeattr.AlsoBuy::showalowbuypage(I)(pagenum);

		}
}-*/;
	void showalowbuypage(int pagenum)
	{
		Window.alert("alert"+pagenum);
		getImagestore().getStoreinfo().setStart((pagenum*limit)+"");
		getImagestore().reload();
	}
	void showalowbuy(String b2cgcode)
	{
		Window.alert("alert");
		
	}
	
}
